﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace EcSalseManage.App_Code
{
    public class CommonFunction
    {
        public static string LcsMessageKey
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["LcsMessageKey"].ToString(); }
        }


        /// <summary>
        /// Reporting Server路径
        /// </summary>
        public static string Reporting_Server
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["Reporting"].ToString(); }
        }

        #region 权限范围
        public static bool isInPermExtent(string entity_Emp_Code, string entity_Dept_Code)
        {
            bool isInExtent = false;
            string[] arrDept = entity_Dept_Code.ToString().Split(new char[] { ',' });
            string[] arrEmp = entity_Emp_Code.ToString().Split(new char[] { ',' });

            if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("全部") > -1)
            {
                isInExtent = true;
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本部门及下属部门") > -1)
            {
                for (int i = 0; i < arrDept.Length; i++)
                {
                    if (arrDept[i].ToString().IndexOf(System.Web.HttpContext.Current.Session["Dept_Code"].ToString()) > -1)
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本部门") > -1)
            {
                for (int i = 0; i < arrDept.Length; i++)
                {
                    if (arrDept[i].ToString() == System.Web.HttpContext.Current.Session["Dept_Code"].ToString().ToString())
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本人") > -1)
            {
                for (int i = 0; i < arrEmp.Length; i++)
                {
                    if (arrEmp[i].ToString() == System.Web.HttpContext.Current.Session["Emp_Code"].ToString())
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }

            return isInExtent;
        }

        public static bool isInPermExtentByBias(string entity_Emp_Code, string entity_Dept_Code)
        {
            bool isInExtent = false;
            string[] arrDept = entity_Dept_Code.ToString().Split(new char[] { '\\' });
            string[] arrEmp = entity_Emp_Code.ToString().Split(new char[] { '\\' });

            if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("全部") > -1)
            {
                isInExtent = true;
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本部门及下属部门") > -1)
            {
                for (int i = 0; i < arrDept.Length; i++)
                {
                    if (arrDept[i].ToString().IndexOf(System.Web.HttpContext.Current.Session["Dept_Code"].ToString()) > -1)
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本部门") > -1)
            {
                for (int i = 0; i < arrDept.Length; i++)
                {
                    if (arrDept[i].ToString() == System.Web.HttpContext.Current.Session["Dept_Code"].ToString().ToString())
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }
            else if (System.Web.HttpContext.Current.Session["Extent"].ToString().IndexOf("本人") > -1)
            {
                for (int i = 0; i < arrEmp.Length; i++)
                {
                    if (arrEmp[i].ToString() == System.Web.HttpContext.Current.Session["Emp_Code"].ToString())
                    {
                        isInExtent = true;
                        break;
                    }
                }
            }

            return isInExtent;
        }
        #endregion

        #region 权限验证
        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="entity_Emp_Code"></param>
        /// <param name="entity_Dept_Code"></param>
        /// <returns></returns>
        public static bool isInPrev(string entity_Emp_Code, string entity_Dept_Code, string prev)
        {
            bool isReturn = false;
            try
            {
                string[] arrRole = System.Web.HttpContext.Current.Session["Roles"].ToString().Split(new char[] { ',' });
                bool isInExtent = false;

                isInExtent = isInPermExtent(entity_Emp_Code, entity_Dept_Code);

                if (isInExtent)
                {
                    if (System.Web.HttpContext.Current.Session["Perm"].ToString().IndexOf(prev) > -1)
                    {
                        isReturn = true;
                    }
                }

            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message.ToString());
            }
            return isReturn;

        }

        public static bool isInPrevByBias(string entity_Emp_Code, string entity_Dept_Code, string prev)
        {
            bool isReturn = false;
            try
            {
                string[] arrRole = System.Web.HttpContext.Current.Session["Roles"].ToString().Split(new char[] { ',' });
                bool isInExtent = false;

                isInExtent = isInPermExtentByBias(entity_Emp_Code, entity_Dept_Code);

                if (isInExtent)
                {
                    if (System.Web.HttpContext.Current.Session["Perm"].ToString().IndexOf(prev) > -1)
                    {
                        isReturn = true;
                    }
                }

            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message.ToString());
            }
            return isReturn;

        }

        public static bool isInPrev(string prev)
        {
            bool isReturn = false;
            try
            {
                if (System.Web.HttpContext.Current.Session["Purview"].ToString().IndexOf(prev) > -1)
                {
                    isReturn = true;
                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message.ToString());
            }

            return isReturn;

        }
        #endregion

        #region 检查是否符合录入时间

        public static bool chkDate()
        {
            int iMonthEnd = DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays(-1).Day;

            //if (DateTime.Now.DayOfWeek == DayOfWeek.Monday && iMonthEnd != DateTime.Now.Day)
            //{
            //    if (Convert.ToDateTime(DateTime.Now) >= Convert.ToDateTime("10:30") && Convert.ToDateTime(DateTime.Now) <= Convert.ToDateTime("14:00"))
            //    { return true; }
            //    else
            //    { return false;}
            //}
            if (iMonthEnd == DateTime.Now.Day)
            {
                if (Convert.ToDateTime(DateTime.Now) >= Convert.ToDateTime("18:30") && Convert.ToDateTime(DateTime.Now) <= Convert.ToDateTime("23:59"))
                { return true; }
                else
                { return false; }
            }
            //else if (DateTime.Now.Day == 1)
            //{return true;}
            else
            { return false; }
        }
        #endregion

        #region 检查导报表时间

        public static bool chkReportDate()
        {
            int iMonthEnd = DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays(-1).Day;

            if (iMonthEnd == DateTime.Now.Day)
            {
                if (Convert.ToDateTime(DateTime.Now) >= Convert.ToDateTime("12:30") && Convert.ToDateTime(DateTime.Now) <= Convert.ToDateTime("18:30"))
                { return false; }
                else
                { return true; }
            }
            else
            { return true; }
        }
        #endregion

        #region 取月头

        public static DateTime GetDayBegin(DateTime dtDate)
        {
            DateTime dtBegin;

            dtBegin = dtDate;
            dtBegin = Convert.ToDateTime(dtBegin.Year + "-" + dtBegin.Month + "-1");
            return dtBegin;
        }
        #endregion

        #region 取月底

        public static DateTime GetDayEnd(DateTime dtDate)
        {
            DateTime dtEnd;

            dtEnd = dtDate;
            if (dtEnd.Month == 2)
                if (dtEnd.Year % 4 == 0)
                    dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-29");
                else
                    dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-28");
            else
                if (dtEnd.Month < 7)
                    if (dtEnd.Month % 2 == 1)
                        dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-31");
                    else
                        dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-30");
                else
                    if (dtEnd.Month == 7)
                        dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-31");
                    else
                        if (dtEnd.Month % 2 == 1)
                            dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-30");
                        else
                            dtEnd = Convert.ToDateTime(dtEnd.Year + "-" + dtEnd.Month + "-31");

            return dtEnd;
        }
        #endregion

        #region 文件类型检查
        private static string[] _fileType = new string[] { ".jpg", ".jpeg", ".xls", ".doc", ".ppt", ".pdf", ".xlsx", ".docx", ".pptx", ".gif", ".png", ".wps" };
        /// <summary>
        /// 检查上传文件类型,如果可以上传,既返回改文件名的后缀名,否则返回"";
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string CheckFileType(string fileName)
        {
            foreach (string t in _fileType)
            {
                if (fileName.ToLower().EndsWith(t))
                {
                    return t;
                }
            }
            return "";
        }

        /// <summary>
        /// 检查文件大小
        /// </summary>
        /// <param name="fileSize">文件大小,字节为单位</param>
        /// <param name="sizeM">限制大小,M为单位</param>
        /// <returns></returns>
        public static bool CheckFileSize(long fileSize, int sizeM)
        {
            return fileSize < (1048576 * sizeM);
        }

        #endregion

        #region 发送邮件提醒（增编申请审批）
        /// <summary>
        /// 发送邮件提醒（增编申请审批）
        /// </summary>
        public static void SendMessage(string sReceiveEmpName, string sReceiveEmpCode, string strTitle, string strBody)
        {
            #region 变量定义
            DataAccess.Operate.DA_Employee_Inherit _Employee_Inherit = new DataAccess.Operate.DA_Employee_Inherit();

            EmailService.EmailService emailService = new EmailService.EmailService();
            DataAccess.SendMessage.LCSService lcsService = new DataAccess.SendMessage.LCSService();

            EcSalseManage.wsKDHR.Service hrService = new EcSalseManage.wsKDHR.Service();
            #endregion

            try
            {
                #region 获取邮件地址
                string strEmail = _Employee_Inherit.getDomainUserEmail(sReceiveEmpName, false);
                #endregion

                #region 发送信息
                //如果邮箱不为空，而且参数指定要发送才发送邮件
                if (strEmail.Length > 0)
                {
                    try
                    {
                        strBody += "<br/>-此消息由[销售管理系统]发出，不需回复！谢谢！";

                        //#region 测试的时候只发给自己
                        //if (true)
                        //{
                        //    strBody += strEmail;
                        //    strEmail = "tianchao@centaline.com.cn";
                        //}
                        //#endregion

                        emailService.HTMLFormatSend(strEmail, strTitle, strBody);
                        lcsService.SendMessage(LcsMessageKey, strEmail, "", strBody);
                    }
                    catch
                    {
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                emailService = null;
                lcsService = null;
                hrService = null;
            }
        }
        #endregion


    }
}
